import { post, apiPaths } from '../../utils/request.js'

Page({
  data: {
    taskList: [],
    statusFilter: 'all',
    priorityFilter: 'all',
    searchKeyword: '',
    currentFilter: 'all',
    currentSort: 'created'
  },

  onLoad(options) {
    this.loadTaskList()
  },

  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    })
    this.loadTaskList()
  },

  // 添加这个方法来处理筛选变化
  onFilterChange(e) {
    const filter = e.currentTarget.dataset.filter
    this.setData({
      currentFilter: filter
    })
    
    // 根据筛选类型设置相应的筛选器
    if (filter === 'mine') {
      // 这里可以设置只显示当前用户任务的筛选条件
      this.loadTaskList()
    } else {
      // 设置状态筛选器
      this.setData({
        statusFilter: filter
      })
      this.loadTaskList()
    }
  },

  onSortChange(e) {
    const sort = e.currentTarget.dataset.sort
    this.setData({
      currentSort: sort
    })
    this.loadTaskList()
  },

  onStatusFilterChange(e) {
    this.setData({
      statusFilter: e.currentTarget.dataset.status
    })
    this.loadTaskList()
  },

  onPriorityFilterChange(e) {
    this.setData({
      priorityFilter: e.currentTarget.dataset.priority
    })
    this.loadTaskList()
  },

  loadTaskList() {
    // 调用获取任务列表接口
    post(apiPaths.getTasks).then(res => {
      if (res.code === 200) {
        let tasks = res.data || []
        
        // 根据搜索关键词过滤
        if (this.data.searchKeyword) {
          tasks = tasks.filter(task => 
            task.title.includes(this.data.searchKeyword) ||
            (task.description && task.description.includes(this.data.searchKeyword))
          )
        }
        
        // 根据状态过滤
        if (this.data.currentFilter !== 'all' && this.data.currentFilter !== 'mine') {
          tasks = tasks.filter(task => {
            // 将任务状态映射到筛选器值
            let taskStatus = task.status
            if (taskStatus === '进行中') taskStatus = 'in-progress'
            else if (taskStatus === '待处理') taskStatus = 'pending'
            else if (taskStatus === '待测试') taskStatus = 'testing'
            else if (taskStatus === '已完成') taskStatus = 'completed'
            
            return taskStatus === this.data.currentFilter
          })
        }
        
        // 根据"我的任务"筛选
        if (this.data.currentFilter === 'mine') {
          // 这里应该根据当前用户ID来筛选任务
          // 暂时使用模拟逻辑
          tasks = tasks.filter(task => task.assigneeId === 1) // 假设当前用户ID为1
        }
        
        // 根据优先级过滤
        if (this.data.priorityFilter !== 'all') {
          tasks = tasks.filter(task => task.priority === this.data.priorityFilter)
        }
        
        // 添加排序逻辑
        if (this.data.currentSort === 'priority') {
          tasks.sort((a, b) => {
            const priorityMap = {'紧急': 4, '高': 3, '中': 2, '低': 1}
            return priorityMap[b.priority] - priorityMap[a.priority]
          })
        } else if (this.data.currentSort === 'created') {
          tasks.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
        } else if (this.data.currentSort === 'updated') {
          tasks.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt))
        }
        
        // 添加样式类
        tasks = tasks.map(task => {
          return {
            ...task,
            statusClass: this.getStatusClass(task.status),
            priorityClass: this.getPriorityClass(task.priority)
          }
        })
        
        this.setData({
          taskList: tasks
        })
      } else {
        wx.showToast({
          title: res.message || '获取任务列表失败',
          icon: 'none'
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message || '获取任务列表失败',
        icon: 'none'
      })
    })
  },
  
  getStatusClass(status) {
    switch (status) {
      case '已完成':
        return 'completed'
      case '处理中':
        return 'in-progress'
      case '待测试':
        return 'pending'
      default:
        return 'pending'
    }
  },
  
  getPriorityClass(priority) {
    switch (priority) {
      case '紧急':
        return 'urgent'
      case '高':
        return 'high'
      case '中':
        return 'medium'
      case '低':
        return 'low'
      default:
        return 'medium'
    }
  },

  onTaskTap(e) {
    const taskId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/taskDetail/taskDetail?id=${taskId}`
    })
  },

  onCreateTask() {
    wx.navigateTo({
      // url: '/pages/taskDetail/taskDetail?action=create'
      url: '/pages/createTask/createTask?action=create'
    })
  }
})